package org.example.ims.Controller.Order;

import org.example.ims.DTO.Response;
import org.example.ims.Entity.Order.OutStockOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.example.ims.Service.Order.OutStockOrderService;


@RestController
@RequestMapping("/outStockOrder")
public class OutStockOrderController {

    @Autowired
    private OutStockOrderService outStockOrderService;

    @PostMapping
    public ResponseEntity<OutStockOrder> createOrder(@RequestBody OutStockOrder order) {
        OutStockOrder createdOrder = outStockOrderService.createOrder(order);
        return ResponseEntity.ok(createdOrder);
    }

    @PostMapping("/{orderId}/execute")
    public ResponseEntity<OutStockOrder> executeOrder(@PathVariable Integer orderId) {
        outStockOrderService.executeOrder(orderId);
        return ResponseEntity.noContent().build();
    }

    @PutMapping("/{orderId}")
    public ResponseEntity<OutStockOrder> updateOrder(@PathVariable Integer orderId, @RequestBody OutStockOrder order) {
        OutStockOrder updatedOrder = outStockOrderService.updateOrder(orderId, order);
        return ResponseEntity.ok(updatedOrder);
    }

    @DeleteMapping("/{orderId}")
    public ResponseEntity<Void> deleteOrder(@PathVariable Integer orderId) {
        outStockOrderService.deleteOrder(orderId);
        return ResponseEntity.noContent().build();
    }

    @GetMapping("/{orderId}")
    public ResponseEntity<OutStockOrder> getOrderById(@PathVariable Integer orderId) {
        OutStockOrder order = outStockOrderService.getOrderById(orderId);
        return ResponseEntity.ok(order);
    }

    @GetMapping("/search")
    public ResponseEntity<Page<OutStockOrder>> getAllOrders(
            @RequestParam(defaultValue = "0") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<OutStockOrder> orders = outStockOrderService.getAllOrders(page, size);
        return ResponseEntity.ok(orders);
    }

    @GetMapping
    public ResponseEntity<Response<Page<OutStockOrder>>> searchOrders(
            @RequestParam(required = false) String status,
            @RequestParam(required = false) Integer customerId,
            @RequestParam(required = false) Integer salesmanId,
            @RequestParam(required = false) Integer warehouseId,
            @RequestParam(defaultValue = "0") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<OutStockOrder> orders = outStockOrderService.searchOrders(status, customerId, salesmanId, warehouseId, page, size);
        return ResponseEntity.ok(Response.success(orders));
    }
}